// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of CXformsElemetnModel.
//   $Id$


#pragma once
#ifndef __MODEL_H_
#define __MODEL_H_ 1

#include "XFormsElementDefaultImpl.h"
#include "parseModel.h"
#include "Model.h"

#include "Common/COM/msw/MultiIDispatch.h"


#define CXFORMSELEMENTMODEL_EVENTS_LENGTH 15

typedef CMultiIDispatch<CXFormsElementModel> CModelAsMultiIDispatch;
typedef IDispatchImpl<IXFormsModelElement, &IID_IXFormsModelElement, &LIBID_XFORMSLib> ModelDispatch1;
typedef IDispatchImpl<IEventTarget, &IID_IEventTarget, &LIBID_DOM2EventsLib, 2,0> ModelDispatch2;
typedef IDispatchImpl<IXFormsModelElement2, &IID_IXFormsModelElement2, &LIBID_XFORMSLib,1,17> ModelDispatch3;

class ATL_NO_VTABLE CXFormsElementModel 
	:public CXFormsElementInvisibleDefaultImpl<CImplModel,CXFormsParseModel,CXFormsElementModel>
	,public ModelDispatch1
	,public ModelDispatch3
	,public CMultiIDispatch<CXFormsElementModel>
//	,public IUIModelEvents
{
public:
	CXFormsElementModel();
	virtual ~CXFormsElementModel();

#ifdef _DEBUG
	ULONG InternalRelease( void)
	{
		ULONG l = __super::InternalRelease();
		return l;
	}

	ULONG InternalAddRef()
	{
		ULONG l = __super::InternalAddRef();
		return l;
	}
#endif

	DECLARE_PROTECT_FINAL_CONSTRUCT()
	
	//COM interface declarations for script accessible methods
#pragma warning(disable:4244)
	BEGIN_MULTI_DISPATCH_MAP(CXFormsElementModel)
		MULTI_DISPATCH_ENTRY(ModelDispatch3)
		MULTI_DISPATCH_ENTRY(ModelDispatch1)
		MULTI_DISPATCH_ENTRY(ModelDispatch2)
	END_MULTI_DISPATCH_MAP()
#pragma warning(default:4244)
//DECLARE_MULTI_DISPATCH(CXFormsElementModel)
 STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames, \
                            LCID lcid, DISPID* rgdispid) \
	{ \
	if(m_bParseDone)
	{
		return CModelAsMultiIDispatch::GetIDsOfNames(riid, rgszNames, cNames, \
                                              lcid, rgdispid); \
	}
	else
	{
		return DISP_E_UNKNOWNNAME;
	}

	} \
   STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid, \
                     LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult, \
                     EXCEPINFO* pexcepinfo, UINT* puArgErr) \
	{ \
      return CModelAsMultiIDispatch::Invoke(dispidMember, riid, lcid, wFlags, \
                                       pdispparams, pvarResult, \
                                       pexcepinfo, puArgErr); \
	}

	BEGIN_COM_MAP(CXFormsElementModel)
		COM_INTERFACE_ENTRY(IXFormsModelElement)
		COM_INTERFACE_ENTRY(IXFormsModelElement2)
		COM_INTERFACE_ENTRY2(IDispatch,IXFormsModelElement)
		COM_INTERFACE_ENTRY_CHAIN(CXFormsElement)
	END_COM_MAP()

	//script accessible methods
	STDMETHOD(getInstanceDocument)(VARIANT vInstanceId, IXMLDOMDocument** ppDoc);
	STDMETHOD(getInstanceDocument)(BSTR sInstanceId, IXMLDOMDocument** ppDoc);
    STDMETHOD(rebuild)(void);
    STDMETHOD(recalculate)(void);
    STDMETHOD(revalidate)(void);
	STDMETHOD(refresh)(void);
	STDMETHOD(putInstanceDocument)(BSTR instanceId, IXMLDOMDocument *pDocument);
	STDMETHOD(get_instances)(IXFormsInstanceCollection **ppInstances);
	STDMETHOD(getXmlState)(IXmlState **ppXmlState);

public:
	virtual HRESULT InitBehaviour();
/*
	// interface IUIModelEvents

	//Construction/destruction events.
	virtual HRESULT onModelConstruct ();
	virtual HRESULT onModelConstructDone ();
	virtual HRESULT onReady ();
	virtual HRESULT onModelDestruct ();

	//errors
	virtual HRESULT onBindingException ();
	virtual HRESULT onLinkError();
	virtual HRESULT onLinkException();
	virtual HRESULT onComputeException();

	//These are actually targeted at instance
	//	but instance does not have a viewlink.
	virtual HRESULT onInsert();
	virtual HRESULT onDelete();

	//re... events

	virtual HRESULT onRebuild();
	virtual HRESULT onRecalculate();
	virtual HRESULT onRevalidate();
	virtual HRESULT onRefresh();
	virtual HRESULT onReset();
*/
	virtual HRESULT ElementReady(void); // Added for bug 267
	virtual HRESULT DocumentReady(void); // Added for bug ???

protected:
	virtual HRESULT InsertBranding(void) { /* NO BRANDING ON MODEL */ return S_FALSE; }
	virtual void RegisterFindBehaviourRefusal();
	virtual void RegisterFindBehaviourAcceptance();

private:
	bool m_bParseDone;
	//array storing event cookies.
	//	these are the ids given to us by 
	//	RegisterEvent.
	long m_alEventCookies[CXFORMSELEMENTMODEL_EVENTS_LENGTH];
	//The event cookies are assigned on an
	//	object-by-object basis, however, 
	//	the names can be static.
	static const CComBSTR * const sm_asEventNames[CXFORMSELEMENTMODEL_EVENTS_LENGTH];

	bool m_bSoftbarInvoked;

	
};
OBJECT_ENTRY_AUTO(__uuidof(XFormsModelElement), CXFormsElementModel)
#endif